#
data = require 'gulp-data'
stylus = require 'gulp-stylus'
rename = require 'gulp-rename'
path = require 'path'

replace = require 'gulp-replace'

module.exports = (gulp) ->
  # 直接编译
  gulp.task 'compress-stylus', ['bower:install', 'copy:bower'], ->
    return gulp.src './stylus/sys/build.styl'
      .pipe stylus
        paths: [(path.resolve 'stylus'), (path.resolve 'static/vendor')]
        'include css': true
      .pipe replace /url\(default\//g,'url(/vendor/layer/skin/default/'
      .pipe gulp.dest('./static/sys/css')

  # 压缩编译
  gulp.task 'compress-stylus:dist', ['bower:install', 'clean:dist', 'copy:bower'], ->
    return gulp.src './stylus/sys/build.styl'
      .pipe stylus
        compress: true
        paths: [(path.resolve 'stylus'), (path.resolve 'static/vendor')]
        'include css': true
      .pipe replace /url\(default\//g,'url(/vendor/layer/skin/default/'
      .pipe rename(extname: '.min.css')
      .pipe gulp.dest('./static-dist/sys/css')


  # runtime 压缩编译 根据package.json的启动脚步 匹配 -p demo1 & 之间的值
  gulp.task 'compress-stylus:runtime', ['bower:install', 'clean:dist', 'copy:bower'], ->
    project = require('../package.json').scripts.dev.match(/-p(.*?)&/)[1].trim()
    console.log path.resolve project
    return gulp.src "./#{project}/stylus/build.styl"
      .pipe stylus
        compress: true
        paths: [(path.resolve project), (path.resolve 'static/vendor')]
        'include css': true
      .pipe replace /url\(default\//g,'url(/vendor/layer/skin/default/'
      .pipe rename(extname: '.min.css')
      .pipe gulp.dest('./static-dist/sys/css')
